﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ASBCC.Database.Entity;
using SourceCode.Workflow.Client;
using ASBCC.Database.Service;

namespace ASBCC.Workflow.K2Manager
{
    public class ForwardBackActionHandler : AskForMoreActionHandler
    {
        #region Constructor

        public ForwardBackActionHandler(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers,string ccUser) :
            base(dataFields, appForm, workItem, actionInfo, comment, auditDestination, outputDestinationUsers,ccUser)
        {
        }

        #endregion

        protected override void SendMail()
        {
            string actionType = GetActionType();
            string ApplicationNameEn = FlowProcess.ProcDisplayEn;
            string ApplicationNameCn = FlowProcess.ProcDisplayCn;

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(WorkItem.ProcName, WorkItem.ActivityName, actionType);
            if (mailConfig == null) return;

            string auditUser = (AppForm.DelegateFrom.UserId <= 0 || AppForm.DelegateFrom.UserId == AppForm.CurrentUser.UserId) ?
                    AppForm.CurrentUser.UserEn : string.Format("{0}({1})", AppForm.CurrentUser.UserEn, AppForm.DelegateFrom.UserEn);

            // 通知下一步审批人
            if (NewMyActivityList != null && NewMyActivityList.Count > 0)
            {
                foreach (MyActivityTableEntity myActivity in NewMyActivityList)
                {
                    if (myActivity.OwnerID == K2User.TempUser) continue; //临时用户不需要通知

                    int toAuditorTemplate = mailConfig.ToAuditorTemplate ?? 0;
                    if (toAuditorTemplate <= 0) continue;

                    EmployeeTableEntity ownerInfo = K2DataHelper.GetUserInfoById(myActivity.OwnerID ?? 0);

                    if (!CheckMailApproverRule(ownerInfo)) continue;

                    string[] mailAddressArr = new string[] { ownerInfo.Email };
                    NameValueCollection collection = new NameValueCollection();
                    collection["{EnglishName}"] = myActivity.OwnerEn;
                    collection["{SerialNumber}"] = myActivity.ApplicationNo;
                    collection["{AuditUser}"] = auditUser;
                    collection["{FormURL}"] = myActivity.FormURL;
                    collection["{Comments}"] = Comment;
                    collection["{AuditTime}"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    collection["{ApplicationNameEn}"] = ApplicationNameEn;
                    collection["{ApplicationNameCn}"] = ApplicationNameCn;
                    collection["{ModuleName}"] = AppForm.ModuleName;

                    NameValueCollection titleCollection = new NameValueCollection();
                    titleCollection["{ApplicationNameEn}"] = ApplicationNameEn;
                    titleCollection["{SerialNumber}"] = myActivity.ApplicationNo;

                    MailService.MailSender(mailAddressArr, toAuditorTemplate, collection, titleCollection);
                }
            }
        }

        protected override string GetActionType()
        {
            return K2ActionType.ForwardBack;
        }
    }
}
